﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.IO;
using System.Text;
using System.Xml.Serialization;
using ScrumTable.Common;
using ScrumTable.Common.FileSystem;

#endregion

namespace ScrumTable.Common.Config.Xml
{
    /// <summary>
    /// Represents the DriverConfigSerializer xml object serializer.
    /// </summary>
    public class XmlConfigSerializer<T>
        where T : IXmlConfigSection, new()
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor

        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlConfigSerializer{T}"/> class.
        /// </summary>
        public XmlConfigSerializer()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Serializes the file with the given file path and writes down the given data.
        /// </summary>
        /// <param name="storagePath">Specifies the path of the config file to store.</param>
        /// <param name="toStore">Specifies the object which should be stored to the given path.</param>
        /// <returns>Returns the deserialized configuration instance.</returns>
        public void SerializeFile(string storagePath, T toStore)
        {
            PreCondition.AssertNotNullOrEmpty(storagePath, "storagePath");

            using (var file = FileInfoUtil.OpenNewForWriting(storagePath))
            {
                CreateSerializer().Serialize(file, toStore);
            }
        }

        /// <summary>
        /// Deserializes the file with the given file path and fills its data into an instance of T.
        /// </summary>
        /// <param name="storagePath">Specifies the path of the config file to load.</param>
        /// <returns>Returns the deserialized configuration instance.</returns>
        public T DeserializeFile(string storagePath)
        {
            PreCondition.AssertNotNullOrEmpty(storagePath, "storagePath");

            using (var file = new FileStream(storagePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                return PostProcessDeserializedData(CreateSerializer().Deserialize(file));
            }
        }

        /// <summary>
        /// Deserializes the given string and fills its data into an instance of T.
        /// </summary>
        /// <param name="dataToDeserialize">Specifies the data of the config to deserialize.</param>
        /// <returns>Returns the deserialized configuration instance.</returns>
        public T Deserialize(string dataToDeserialize)
        {
            PreCondition.AssertNotNullOrEmpty(dataToDeserialize, "dataToDeserialize");

            using (var data = new StringReader(dataToDeserialize))
            {
                return PostProcessDeserializedData(CreateSerializer().Deserialize(data));
            }
        }

        private T PostProcessDeserializedData(object deserializedObjectModel)
        {
            if (deserializedObjectModel is XmlConfigSectionBase)
            {
                ((XmlConfigSectionBase)deserializedObjectModel).EnsureDataLoaded();
            }
            return (T)deserializedObjectModel;
        }

        private XmlSerializer CreateSerializer()
        {
            return new XmlSerializer(typeof(T));
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
